import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../views/home/Home.vue'
import ScenicList from '../views/scenic/ScenicList.vue'
import UserManagement from '../views/user/UserManagement.vue'
import role from '../views/role/role.vue'
import ticket from '../views/ticket/ticket.vue'
import MenuManagement from '../views/menu/MenuManagement.vue'
import MemberCard from '../views/card/MemberCard.vue'
import cardDetails from '../views/card/cardDetails.vue'
import User from '../views/user/User.vue'
import CollectionManagement from '../views/collection/CollectionManagement.vue'
import Alipay from '../views/alipay/Alipay.vue'
import ScenicOrder from '../views/scenic/ScenicOrder.vue'
import UserRealBinds from '../views/user/UserRealBinds.vue'
import CommentList from '../views/comment/CommentList.vue'
import TravelsList from '../views/travels/TravelsList.vue'
import login from '../views/login/Login.vue'
import { Message } from 'element-ui';

Vue.prototype.$message = Message;
Vue.use(VueRouter)

//解决重复点击导航时，控制台出现报错
//Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}


const routes = [
  // 登录 gpd 2022-08-22 09点58分
  {
    path: '/',
    name: 'login',
    component: login,
  },
  {
    path: '/home',
    name: 'home',
    component: home,
    children: [
      {
        path: '/ScenicList',
        name: 'ScenicList',
        component: ScenicList
      },
      {
        path: '/ScenicOrder',
        name: 'ScenicOrder',
        component: ScenicOrder
      },
      {
        path: '/UserManagement',
        name: 'UserManagement',
        component:UserManagement
      },
      {
        path: '/role',
        name: 'role',
        component:role
      },
      {
        path: '/ticket',
        name: 'ticket',
        component:ticket
      },
      {
        path: '/menu',
        name: 'MenuManagement',
        component: MenuManagement
      },
      {
        path: '/card',
        name: 'MemberCard',
        component: MemberCard
      },
      {
        path: '/cardDetails',
        name: 'cardDetails',
        component: cardDetails
      },
      {
        path: '/User',
        name: 'User',
        component: User
      },
      {
        path: '/TravelsList',
        name: 'TravelsList',
        component: TravelsList
      },
      {
        path: '/CommentList',
        name: 'CommentList',
        component: CommentList
      },
      {
        path: '/collectionManagement',
        name: 'CollectionManagement',
        component: CollectionManagement
      },
      {
        path: '/Alipay',
        name: 'Alipay',
        component: Alipay
      },
      // 绑定实名信息 lzy 2022-8-17 11点20分
      {
        path: '/UserRealBinds',
        name: 'UserRealBinds',
        component: UserRealBinds
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  var token = window.sessionStorage.getItem("token")
  if(to.path==="/"){
    next()
  }
  else{
    if(!token){
        window.sessionStorage.setItem("toPath",to.path)
        Message({
            message: "请您先登录账户",
            type: 'warning'
        })
        next("/")
    }
    else{
      next()
    }
  }
})

export default router
